<?php
defined('IN_PHPCMS') or exit('No permission resources.');
define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
$session_storage = 'session_'.pc_base::load_config('system','session_storage');
pc_base::load_sys_class($session_storage);
class index {
	private $db, $m_db, $M,$mo_db,$tablename;
	function __construct() {
		$this->db = pc_base::load_model('sitemodel_model');
		$this->tea_idb = pc_base::load_model('teacherinfo_model');
		$this->tea_adb=pc_base::load_model('teacheranswer_model');
		$this->m_db = pc_base::load_model('sitemodel_field_model');

		//fool,读取缓存
		$this->M = new_html_special_chars(getcache('formguide', 'commons'));

		$this->siteid = intval($_GET[siteid]) ? intval($_GET[siteid]) : get_siteid();
		$this->M = $this->M[$this->siteid];
	}
	

	public function tpl($file, $m = '') {
		$m = empty($m) ? ROUTE_M : $m;
		if(empty($m)) return false;
		return PC_PATH.'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$file.'.tpl.php';
	}

	public function teacher_login(){
		$formid=$_GET['formid'];
		$batch=$_GET['batch'];
		include template('formguide', 'teacher_login');
	}

	public function loginout(){
		session_destroy();
		echo "<script>location.href='index.php'</script>";
	}

	public function teacher_logincheck(){
		//验证教师数据
		if(!isset($_POST['userNumber'])){
			echo "<script>alert('工号不能为空!');history.go(-1)</script>";
		}
		if(empty($_POST['password'])){
			echo "<script>alert('密码不能为空!');history.go(-1)</script>";
		}

		$code = isset($_POST['code']) && trim($_POST['code']) ? trim($_POST['code']) : showmessage(L('input_code'), HTTP_REFERER);
		if ($_SESSION['code'] != strtolower($code)) {
				showmessage(L('code_error'), HTTP_REFERER);
			}

		//批次
		$batch=$_POST['batch'];
		$data = $this->tea_idb->get_one(array('number' =>$_POST['userNumber'] , 'batch' =>$batch));

		if(empty($data)){
				echo "<script>alert('工号不存在!');history.go(-1)</script>";
		}else{
			if($_POST['password'] !=$data['password']){
					echo "<script>alert('密码错误!');history.go(-1)</script>";
			}else{
				//验证正确
				session_start();
				$_SESSION['number']=$_POST['userNumber'];
				//重新读formid，防止url泄露
				$j = $this->db->get_one(array('batch'=>$_POST['batch']));
				$formid=$j['modelid'];
				$number=$_POST['userNumber'];
				echo "<script>location.href='index.php?m=formguide&c=index&a=teacher_show_read&formid=$formid&number=$number'</script>";
			}
		}
	}

	public function teacher_show(){
		//是否已经禁用
		if(isset($_GET['formid'])){
			$formid=$_GET['formid'];
		}
		$j = $this->db->get_one(array('modelid'=>$formid, 'siteid'=>$this->siteid, 'disabled'=>0));
		if (!$j) {
			showmessage(L('form_no_exist'), HTTP_REFERER);
		}
		//时间限制在配置里面
		$setting = string2array($j['setting']); //时间开启可以用
		if ($setting['enabletime']) {
			if ($setting['starttime']>SYS_TIME || ($setting['endtime']+3600*24)<SYS_TIME) {
				showmessage(L('form_expired'), APP_PATH.'index.php?');
			}
		}

		//批次
		$batch=$j['batch'];

		//登陆	
		echo "<script>location.href='index.php?m=formguide&c=index&a=teacher_login&formid=$formid&batch=$batch'</script>";
	}

	//验证完成，读取数据
	public function teacher_show_read() {
		session_start();
		if(!isset($_SESSION['number'])) {
			echo "<script>alert('你没有访问权限!');history.go(-1)</script>";
		}
		if(!$_GET['formid']){
			showmessage("非法操作",HTTP_REFERER);
		}
		//密码验证正确,$data传来工号，姓名，专业,formid,类型
		else{
			$data = $this->tea_idb->get_one('`number`=\''.$_GET['number'].'\'');
			$number=$_GET['number'];
			$username=$data['username'];
			$major=$data['major'];
			$type=$data['type'];
			//判断是否答过问题
			$an_data=$this->tea_adb->get_one(array('number'=>$number,'formid'=>$_GET['formid']));
			
			if(!empty($an_data)){
				$did = intval($an_data['answerId']);
				$formid = intval($an_data['formid']);
				//寻找到相应表
				$f_info = $this->db->get_one(array('modelid'=>$formid, 'siteid'=>$this->siteid));
				$this->tablename = 'form_'.$f_info['tablename'];
				//自己读取答案字段数据表
				$this->mf_db = pc_base::load_model('sitemodel_field_model');
				$this->mf_db->change_table($this->tablename);
				$info = $this->mf_db->get_one(array('dataid'=>$did));
				//读取答案的html
				pc_base::load_sys_class('form', '', '');
				define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
				require CACHE_MODEL_PATH.'formguide_form.class.php';
				$formguide_form = new formguide_form($formid);
				$info=str_replace("\\","",$info);
				$forminfos_fields = $formguide_form->get($info);
				//获取标题
				$modeldata = $this->db->get_one(array('modelid' => $formid));
				include template('formguide', 'show_teacher_done');
			}
			else{
				$formid=$_GET['formid'];
				$siteid = $_GET['siteid'] ? intval($_GET['siteid']) : 1;
				//寻找到相应表，后面的teacher_show_add才知道添加到哪个表
				$f_info = $this->db->get_one(array('modelid'=>$formid, 'siteid'=>$siteid));
				$tablename = 'form_'.$f_info['tablename'];
				// $forminfos_data存储model_field表中每个问题的属性，并根据$info['formtype']的类型，将setting字段中的问题内容转换成html代码
				pc_base::load_sys_class('form', '', '');
				require CACHE_MODEL_PATH.'formguide_form.class.php';
				$formguide_form = new formguide_form($formid, $no_allowed);
				$forminfos_data = $formguide_form->get();

				//fool
				$SEO = seo($this->siteid, L('formguide'), $name);
	
				//获取问卷标题和描述 
				$modeldata = $this->db->get_one(array('modelid' => $formid));
				include template('formguide', 'show_teacher_nodone');
			}
		}

	}

	//处理教师修改过的数据
	public function teacher_show_update() {
		session_start();
		if(!isset($_SESSION['number'])) {
			echo "<script>alert('你没有访问权限!');history.go(-1)</script>";
		}
		//验证码
		$code = isset($_POST['code']) && trim($_POST['code']) ? trim($_POST['code']) : showmessage(L('input_code'), HTTP_REFERER);
		if ($_SESSION['code'] != strtolower($code)) {
				showmessage(L('code_error'), HTTP_REFERER);
		}

		//答案id
		$did = intval($_POST['did']);
		$formid = intval($_POST['formid']);
		//得到修改后的数据
        $info=$_POST['info'];
		pc_base::load_sys_class('form', '', '');
		define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
		require CACHE_MODEL_PATH.'formguide_input.class.php';
		$formguide_input = new formguide_input($formid);
		$data = new_addslashes($info);
		$data = new_html_special_chars($data);
		$data = $formguide_input->get($data);
		//得到要修改的表
		$f_info = $this->db->get_one(array('modelid'=>$formid, 'siteid'=>$this->siteid));
		$this->tablename = 'form_'.$f_info['tablename'];
		$this->mf_db = pc_base::load_model('sitemodel_field_model');
		$this->mf_db->change_table($this->tablename);
		//更新
		$this->mf_db->update($data, array('dataid'=>$did));
		//修改后将状态变为待审
		$tea_data=$this->tea_adb->get_one(array('formid'=>$formid, 'answerId'=>$did));
		if ($tea_data['pass']) {
			$tea_data['pass']=0;
			$this->tea_adb->update($tea_data,array('formid'=>$formid,'answerId'=>$did));
		}
		session_destroy();
		showmessage(L('update_success'),"index.php?");
	}

	//处理教师添加的数据
	public function teacher_show_add() {
		session_start();
		if(!isset($_SESSION['number'])) {
			echo "<script>alert('你没有访问权限!');history.go(-1)</script>";
		}
		//验证码
		$code = isset($_POST['code']) && trim($_POST['code']) ? trim($_POST['code']) : showmessage(L('input_code'), HTTP_REFERER);
		if ($_SESSION['code'] != strtolower($code)) {
				showmessage(L('code_error'), HTTP_REFERER);
		}

		//得到数据表名
		$tablename = $_POST['tablename'];
		$formid=$_POST['formid'];
		$siteid=$_POST['siteid'];
		//连接相应问卷数据表
		$this->m_db->change_table($tablename);
		//将答案转化为数组并存进数据表
		$data = array();
		require CACHE_MODEL_PATH.'formguide_input.class.php';
		$formguide_input = new formguide_input($formid);
		$data = new_addslashes($_POST['info']);
		$data = new_html_special_chars($data);
		$data = $formguide_input->get($data);
		$data['datetime'] = SYS_TIME;
		$data['ip'] = ip();
		$dataid = $this->m_db->insert($data, true);
		//添加教师答案表，默认为待审核
		$this->tea_adb->insert(array('formid' =>$_POST['formid'] , 'answerid' =>$dataid,'number' =>$_POST['number'],'name' =>$_POST['name'],'major' =>$_POST['major'],'type' =>$_POST['type'],'pass' =>0));
		$this->db->update(array('items'=>'+=1'), array('modelid'=>$formid, 'siteid'=>$this->siteid));
		session_destroy();
		showmessage(L('thanks'), APP_PATH);
	}

	public function teacher_changepassword(){
		if(isset($_GET['number'])){
			$number=$_GET['number'];
		}
		if(isset($_POST['number'])){
			$number=$_POST['number'];
		}
		if(isset($_GET['batch'])){
			$batch=$_GET['batch'];
		}
		if(isset($_POST['batch'])){
			$batch=$_POST['batch'];
		}
		
		$info = $this->tea_idb->select(array( 'number' => $number,'batch'=>$batch), '*' ,NULL , '`number` ASC');
		
		$ex_password = $info[0]['password'];
		
		if(isset($_POST['dosubmit'])) {
			$an_info = $this->tea_idb->select(array( 'number' => $number), '*' ,NULL , '`number` ASC');
			if($_POST['password']==''||$_POST['new_password']==''||$_POST['a_password']=='') showmessage(L('illegal_operation'));
			if($_POST['password']!=$ex_password) showmessage("输入原密码不正确");
			if($_POST['new_password']!=$_POST['a_password']) showmessage("新密码不匹配");
			$an_info[0]['password'] = $_POST['new_password'];
			if($this->tea_idb->update($an_info[0], array('number' => $number))){
			session_destroy();
			showmessage("成功更改密码", "index.php?m=formguide&c=index&a=teacher_login&batch=$batch");
			}
		} else {
			include template('formguide', 'teacher_changepassword');
		}
	}


	
}
?>